﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using Microsoft.Practices.Composite.Events;
using PJ.Business.SalePurchase;
using PJ.Business.SalePurchase.Sale;
using PJ.Framework;
using PJ.Framework.Common;
using PJ.Presentation.Partner;

namespace PJ.Presentation.SalePurchase.Sale
{
    public class CustomerShoppingCartModel : ShoppingCartModel, ICustomerShoppingCartModel
    {
        public CustomerShoppingCartModel()
        {
        }

        public CustomerShoppingCartModel(string globalId, ICustomerShoppingCartService service)
            : base(globalId, service)
        {
        }

        [Microsoft.Practices.Unity.InjectionConstructor]
        public CustomerShoppingCartModel(ICustomerShoppingCartView view, IEventAggregator aggregator, ICustomerShoppingCartService service)
            : base(view, aggregator, service)
        {
            EventAggregator.GetEvent<CustomerSelectionEvent>().Subscribe(this.SetPartner);
            EventAggregator.GetEvent<PostInvoiceEvent>().Subscribe(PostInvoice);
        }

        public void PostInvoice(string dummy)
        {
            Persist();
            EventAggregator.GetEvent<SaveCustomerShoppingCartEvent>().Publish(Id);
            if (GlobalId == null || GlobalId == "")
            {
                MessageBox.Show("Panier non défini");
                return;
            }
            if (Id == null || Id == "")
            {
                MessageBox.Show("Client non défini");
                return;
            }
            ICustomerShoppingCartItemService items = InjectionBroker.Resolve<ICustomerShoppingCartItemService>();
            string[] fragments = items.GetFragments(Id, PJ.Data.GetDataMode.Default);
            if (fragments == null)
            {
                MessageBox.Show("Le panier est vide");
                return;
            }
            if (fragments.Count() == 0)
            {
                MessageBox.Show("Le panier est vide");
                return;
            }
            try
            {
                IShoppingCart shoppingCart = (IShoppingCart)GetValues();
                if (shoppingCart == null)
                    throw new ApplicationException("shoppingCart argument is null");
                IEnumerable shoppingCartItems = items.GetList(fragments, PJ.Data.GetDataMode.Default);                
                if (shoppingCart.Id == null || shoppingCart.Id.Length == 0)
                    throw new ApplicationException("Undefined Partner Id");
                string partnerId = shoppingCart.Id;
                string reference = (shoppingCart.Reference == null) ? "" : shoppingCart.Reference;
                List<string> itemIds = new List<string>();
                List<string> units = new List<string>();
                List<double> quantities = new List<double>();
                foreach (IShoppingCartItem item in shoppingCartItems)
                {
                    if (item.InventoryItemId == null || item.InventoryItemId.Length == 0)
                        throw new ApplicationException("Undefined item id in a shopping cart line");
                    itemIds.Add(item.InventoryItemId);
                    if (item.UnitId == null || item.UnitId.Length == 0)
                        throw new ApplicationException("Undefined unit in a shopping cart line");
                    units.Add(item.UnitId);
                    if (item.Quantity == 0)
                        throw new ApplicationException("The quantity of zero is not allowed for shopping cart lines");
                    quantities.Add(item.Quantity);
                }

                ICreateInvoiceResult result = InjectionBroker.Resolve<ICreateSaleInvoice>()
                                        .Execute(partnerId, reference, itemIds, quantities, units);
                PDFReader.PrintPdf(result.PdfDocument, 2);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message, "Erreur");
            }

        }
    }
}

